﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.Model;
using MVCClubSite.IDAL;
using System.Data.Linq;

namespace MVCClubSite.LinqToSQLDAL
{
    /// <summary>
    /// Linq To SQL DAL for links
    /// </summary>
    public class Links : ILinks
    {
        DB _db;

        /// <summary>
        /// Default constructor
        /// </summary>
        public Links()
        {
            _db = new DB();
        }

        /// <summary>
        /// Constructor used to pass in a datacontext
        /// </summary>
        /// <param name="db">datacontext for the DAL to use</param>
        public Links(DB db)
        {
            _db = db;
            if (_db == null)
                throw new InvalidOperationException("DB cannot be null.");
        }

        /// <summary>
        /// Method to get links
        /// </summary>
        /// <returns>List of Links</returns>
        public IQueryable<Link> GetLinks()
        {
            var links = from l in _db.cs_Links
                        select l;

            List<Link> returnLinks = new List<Link>();

            foreach (cs_Link link in links)
            {
                Link l = new Link { ID = link.LinkID, Title = link.Title, Description = link.Description, Url = link.Url, Sequence = link.Sequence };

                if (link.Image != null && link.Image.Length > 0)
                    l.Image = link.Image.ToArray();
                else
                    l.Image = null;

                returnLinks.Add(l);
            }

            return (from l in returnLinks
                    select l).AsQueryable();
        }

        /// <summary>
        /// Method to get a link by ID
        /// </summary>
        /// <param name="id">ID of the link</param>
        /// <returns>Link business entity</returns>
        public Link GetLinkByID(int id)
        {
            var link = (from l in _db.cs_Links
                         where l.LinkID == id
                         select l).FirstOrDefault();

            Link returnLink = new Link { ID = link.LinkID, Title = link.Title, Description = link.Description, Url = link.Url, Sequence = link.Sequence };

            if (link.Image != null && link.Image.Length > 0)
                returnLink.Image = link.Image.ToArray();
            else
                returnLink.Image = null;

            return returnLink;
        }

        /// <summary>
        /// Method to insert or update a link
        /// </summary>
        /// <param name="link">Link business entity</param>
        public void Save(Link link)
        {
            using (DB db = new DB())
            {
                cs_Link dbLink = db.cs_Links.Where(l => l.LinkID == link.ID).SingleOrDefault();
                bool isNew = false;
                if (dbLink == null)
                {
                    dbLink = new cs_Link();
                    isNew = false;
                }

                dbLink.Title = link.Title;
                dbLink.Description = link.Description;
                dbLink.Url = link.Url;
                dbLink.Sequence = link.Sequence;
                if (link.Image != null)
                    dbLink.Image = link.Image;
                else
                    dbLink.Image = new Binary(new byte[] { }); // empty byte

                if (isNew)
                    db.cs_Links.InsertOnSubmit(dbLink);

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// Method to delete a link
        /// </summary>
        /// <param name="id">ID of the link to delete</param>
        public void Delete(int id)
        {
            cs_Link dbLink = _db.cs_Links.Where(l => l.LinkID == id).SingleOrDefault();

            if (dbLink != null)
            {
                _db.cs_Links.DeleteOnSubmit(dbLink);
                _db.SubmitChanges();
            }
        }
    }
}
